package com.dinetech.ejecutor;

import java.util.ArrayList;

import com.dinetech.dao.EventoAdapter;
import com.dinetech.dao.GalponAdapter;
import com.dinetech.dao.WSEvento;
import com.dinetech.dao.WSGalpon;
import com.dinetech.entidades.Galpon;
import com.dinetech.entidades.Personal;
import com.dinetech.entidades.Session;
import com.dinetech.entidades.Eventos;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import android.widget.AdapterView.OnItemSelectedListener;


import android.widget.Spinner;

public class VerEvento extends Activity{
	
	ListView listView;
	Spinner spinner;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_item_evento);
		
				//new TaskGalpon().execute();
		cargarSpinner();
	}
	
	
	private void cargarSpinner(){
		spinner = (Spinner) findViewById(R.id.cbxgalpon);

		Session s= Session.getInstance();
		GalponAdapter AdapterGalpon = new GalponAdapter(VerEvento.this, s.getLISTA_GALPONES());
		
		spinner.setAdapter(AdapterGalpon);
		
		spinner.setOnItemSelectedListener(new SpinnerListener());
	}

	
	private class TaskGalpon extends AsyncTask<ArrayList<Galpon>, Void, ArrayList<Galpon>> {

		@Override
		protected ArrayList<Galpon> doInBackground(ArrayList<Galpon>... arg0) {
			// TODO Auto-generated method stub
			//Looper.prepare();
	    	WSGalpon WSGalpon= new WSGalpon();
	    	Personal objPer = Personal.getInstance();
			ArrayList<Galpon> lstGalpon = new ArrayList<Galpon>();
            int IsSucursal = objPer.getIdSucursal();
			lstGalpon = WSGalpon.WSListarGalpon(IsSucursal);
	    	return lstGalpon;

		}

		@Override
		protected void onPostExecute(ArrayList<Galpon> lstGalpon) {
			// TODO Auto-generated method stub
			super.onPostExecute(lstGalpon);
			
			spinner = (Spinner) findViewById(R.id.cbxgalpon);

			GalponAdapter AdapterGalpon = new GalponAdapter(VerEvento.this, lstGalpon);
			
			spinner.setAdapter(AdapterGalpon);
			
			spinner.setOnItemSelectedListener(new SpinnerListener());
		}
	}

	public class ListViewListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View arg1, int position,
				long id) {
			// TODO Auto-generated method stub
			MostarActividad((int)id,parent.getAdapter().getItemViewType(position));
		}


	}
	public void MostarActividad(int id,int codEve){
		
		switch (codEve) {
		case 1:
			Intent i1 = new Intent(VerEvento.this, Evento_Empadre.class);	
			i1.putExtra("IdEvento", id );
			startActivity(i1);
			break;
		case 2:
			Intent i2 = new Intent(VerEvento.this, Evento_Destete.class);	
			i2.putExtra("IdEvento", id );
			startActivity(i2);
			break;
		case 3:
			Intent i3 = new Intent(VerEvento.this, Evento_Mortalidad.class);	
			i3.putExtra("IdEvento", id );
			startActivity(i3);
			break;
		case 4:
			Intent i4 = new Intent(VerEvento.this, Evento_Parto.class);	
			i4.putExtra("IdEvento", id );
			startActivity(i4);
			break;
		case 6:
			Intent i6 = new Intent(VerEvento.this, Evento_Recria.class);	
			i6.putExtra("IdEvento", id );
			startActivity(i6);
			break;
		case 8:
			Intent i8 = new Intent(VerEvento.this, Evento_Translado.class);	
			i8.putExtra("IdEvento", id );
			startActivity(i8);
			break;
		case 9:
			Intent i9 = new Intent(VerEvento.this, Evento_Tratamiento.class);	
			i9.putExtra("IdEvento", id );
			startActivity(i9);
			break;
		case 10:
			Intent i10 = new Intent(VerEvento.this, Evento_Seleccion.class);	
			i10.putExtra("IdEvento", id );
			startActivity(i10);
			break;
		case 12:
			Intent i12 = new Intent(VerEvento.this, Evento_Ingreso.class);	
			i12.putExtra("IdEvento", id );
			startActivity(i12);
			break;
		case 15:
			Intent i15 = new Intent(VerEvento.this, Evento_Censo.class);	
			i15.putExtra("IdEvento", id );
			startActivity(i15);
			break;

		default:
			break;
		}
		
		
	}
	public class SpinnerListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long Iditem) {
			// TODO Auto-generated method stub
			int IdEvento;
			IdEvento = Integer.parseInt( Long.toString(Iditem));	

			new TaskEvento().execute(IdEvento);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}


	}
	
	private class TaskEvento extends AsyncTask<Integer, Void, ArrayList<Eventos>> {

		@Override
		protected ArrayList<Eventos> doInBackground(Integer... arg0) {
			// TODO Auto-generated method stub
			//Looper.prepare();

	    	WSEvento WSEvento= new WSEvento();
			ArrayList<Eventos> lstEventos = new ArrayList<Eventos>();
			int IdGalpon = Integer.parseInt(arg0[0].toString());
			lstEventos = WSEvento.WSListarEventos(IdGalpon);
	    	
	    	return lstEventos;

		}

		@Override
		protected void onPostExecute(ArrayList<Eventos> lstEventos) {
			// TODO Auto-generated method stub
			super.onPostExecute(lstEventos);
			
				listView = (ListView) findViewById(R.id.lsteventos);

				EventoAdapter AdapterEvento = new EventoAdapter(VerEvento.this,lstEventos);
				
				listView.setAdapter(AdapterEvento);
				
				listView.setOnItemClickListener(new ListViewListener());
		}

	}
	
	 @Override 
	 protected void onPause() {
		   super.onPause();
		   
		}
		 
		@Override 
		protected void onResume() {
		   super.onResume();
		   
		}
		 
		@Override 
		protected void onDestroy() {
		  
		   super.onDestroy();
		}
}